package com.woniuxy.threeprojcet.end_water_project_admin.controller;


import com.woniuxy.threeprojcet.end_water_project_admin.domain.Result;
import com.woniuxy.threeprojcet.end_water_project_admin.domain.form.AccountForm;
import com.woniuxy.threeprojcet.end_water_project_admin.domain.form.RegisterForm;
import com.woniuxy.threeprojcet.end_water_project_admin.service.impl.AccountServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 账号表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-08-26
 */
@RestController
@RequestMapping("/account")
@CrossOrigin("*")
@Slf4j
public class AccountController {
    @Resource
    public AccountServiceImpl accountService;

    @PostMapping("login")
    public String login(@RequestBody AccountForm accountForm) {
//        此处的name应该是数字，数据库为account
        log.info("accountForm为{}", accountForm);
        if (accountForm.getAccount() != null && accountForm.getPassword() != null) {
            int i = accountService.login(accountForm);
            return i == 0 ? "没有此用户" : i == 1 ? "登录成功" : "密码错误";
        } else {
            return "请输入账号密码";
        }

    }

    @PostMapping("register")
    public String Register(@RequestBody RegisterForm registerForm){
        log.info("注册用户：{}",registerForm);

        if(registerForm.getAccount()==null&&registerForm.getPassword()==null){
            log.info("参数不符合要求");
            return "请输入正确的账号密码";
        }
        try {
            boolean register = accountService.register(registerForm);
            return register? "注册成功":"注册失败,请更换账号试试";
        } catch (Exception e) {
            log.warn("注册出错");
            return "好像注册失败了";
        }

    }


}

